package com.example.im.ui.fragment

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.im.common.Enumerate
import com.example.im.R
import com.example.im.common.Constants
import com.example.im.common.ImApplication
import com.example.im.entity.*
import com.example.im.ui.adapter.ChatUserAdapter
import com.example.im.mvp.base.BasePresenter
import com.example.im.ui.abstracts.AbstractBaseFragment
import com.example.im.ui.activity.ChatActivity
import com.example.im.ui.activity.SearchActivity
import kotlinx.android.synthetic.main.fragment_main_chat.*
import kotlinx.android.synthetic.main.fragment_main_chat.linear_search
import kotlinx.android.synthetic.main.fragment_main_friend.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlin.collections.ArrayList

class MainChatFragment : AbstractBaseFragment<BasePresenter<*>>(),
    ChatUserAdapter.OnClickItemListener {

    private var adapter: ChatUserAdapter? = null
    private var recordLasts = ArrayList<ChatRecordLast>()

    override val layoutId: Int
        get() = R.layout.fragment_main_chat

    override fun initView() {

        EventBus.getDefault().register(this)

        recordLasts.addAll(ImApplication.appDatabase().chatRecordLastDao().loadAll())
        adapter = ChatUserAdapter((activity as Context?)!!, recordLasts)
        adapter!!.setOnItemClickListener(this)
        recycler_view_chat.layoutManager = LinearLayoutManager(activity)
        recycler_view_chat.itemAnimator = null
        recycler_view_chat.adapter = adapter

        linear_search.setOnClickListener {
            activity!!.startActivity(Intent(activity, SearchActivity::class.java))
        }
    }

    override fun initPresenter() {

    }

    override fun initLazyLoad() {

    }

    override fun onDestroyView() {
        super.onDestroyView()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(eventBus: EventBusData<*>) {
        if (eventBus.type ?: return != Enumerate.Handle.list_chat)
            return
        when (eventBus.action ?: return) {
            Enumerate.Action.list_add -> {
                val map = (eventBus.data ?: return) as MutableMap.MutableEntry<*, *>
                adapter!!.addDataItem(map.key as ChatRecordLast)
            }
            Enumerate.Action.list_updata -> {
                val last = (eventBus.data ?: return) as ChatRecordLast
                adapter!!.upDataItem(last)
            }
            Enumerate.Action.list_delete -> {
                val friend = (eventBus.data ?: return) as FriendOrRoom
                adapter!!.removeDataItem(friend.id)
                onHint("已删除聊天记录")
            }
            Enumerate.Action.list_clear -> {
                adapter!!.clearData()
            }
            else -> {
            }
        }
    }

    override fun onClickItem(view: View, position: Int) {
        val last = recordLasts[position]
        //跳转至聊天页面
        val intent = Intent(activity!!, ChatActivity::class.java)
        val friendOrRoom = ImApplication.appDatabase().friendOrRoomDao()
            .selectFriendOrRoomById(last.fId)
        intent.putExtra(Constants.BUNDLE_T0_CHAT_DATA, friendOrRoom)
        activity!!.startActivity(intent)
    }
}